package com.chinanet.utils.webservice;

import org.apache.commons.lang.StringEscapeUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ITMSWebService {
	/**
	 * Init WebService method
	 * @param
	 * @return
	 */
	public String WebServiceInit(AttrsIn attrs,int type) {
		String returnBack = "";
		try {
			URL url = new URL(
					"http://172.20.4.28:8080/services/TzItmsWSPort");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
			conn.setRequestMethod("POST");
			conn.setRequestProperty("soapAction", "");
			conn.connect();

			String soap="";
			if(type==1){
			 soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.com/\">" +
		            "<soapenv:Header/>" +
		            "<soapenv:Body>" +
		            "<"+attrs.getFunctionName()+">" +
		            "<arg0><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT>" +
		            attrs.getRequestMessage() + 
		            "</ROOT>]]></arg0>" +
		            "</"+attrs.getFunctionName()+">" +
		            "</soapenv:Body>" +
		            "</soapenv:Envelope>";
			}else{
				 soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.com/\">" +
				            "<soapenv:Header/>" +
				            "<soapenv:Body>" +
				            "<"+attrs.getFunctionName()+">" +
				            "<arg0><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT>" +
				            attrs.getRequestMessage() + 
				            "</ROOT>]]></arg0>" +
				            "</"+attrs.getFunctionName()+">" +
				            "</soapenv:Body>" +
				            "</soapenv:Envelope>";
			}
			OutputStream out = conn.getOutputStream();
			out.write(soap.getBytes("UTF-8"));
			out.flush();
			int code = conn.getResponseCode();
			if (code == 200) {
				InputStream in = conn.getInputStream();
				byte b[] = new byte[10240];
				StringBuffer sb = new StringBuffer();
				int len = 0;
				while ((len = in.read(b)) != -1) {
					String str = new String(b, 0, len, "UTF-8");
					sb.append(str);
				}
				String ak = new String(sb.toString());
				ak = ak.replaceAll("&amp;", "");
				ak = ak.replaceAll("&lt;", "<");
				ak = ak.replaceAll("&gt;", ">");
				ak = ak.replaceAll("lt;", "<");
				ak = ak.replaceAll("gt;", ">");
				ak = ak.replaceAll("&quot;", "\"");
				ak = ak.replaceAll("&apos;", "\'");
				ak = ak.replaceAll(";", "");
				ak = ak.replaceAll("&#x", "\\\\u");
				returnBack = StringEscapeUtils.unescapeJava(ak);
				in.close();
			} else if (code == 500) {
				System.out.println("server ERROR!");
				returnBack	="server ERROR!";
			} else if (code == 504) {
				System.out.println("Gateway Timeout!");
				returnBack	="Gateway Timeout!";
			} else if (code == 404) {
				System.out.println("Not Found!");
				returnBack	="Not Found!";
			} else if (code == 408) {
				System.out.println("Request Time-Out!");
				returnBack	="Request Time-Out!";
			} else {
				System.out.println("Connection Failed!");
				returnBack	="Connection Failed!";
			}
			out.close();
			conn.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return returnBack;

	}

	/**
	 *  The content of the find nodes
	 * @param nodeName
	 * @param xml
	 * @return
	 */
	public  String getPattern(String nodeName, String xml) {
		String returnBack = "";
		String regex = "<" + nodeName + ">([\\s\\S].*)</" + nodeName + ">";
		xml=xml.replaceAll("\\r", "");
		xml=xml.replaceAll("\\n", "");
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(xml);
		while (matcher.find()) {
			returnBack = matcher.group(1);
		}
		return returnBack;
	}
	
	public String dealString(String string){
		if(string==null||string.isEmpty()){
			return "";
		}else{
			return string;
		}
	}
	
	public String getop_id(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
		String op_id=sdf.format(new Date())+(int)(Math.random()*100);
		return op_id;
	}
}
